﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class ElementPiece : Item
    {
        public string PieceID
        {
            get;
            set;
        }
        public ElementPiece(World world, Stage stage)
            : base(world, stage)
        {
        }

        public override void CreatBody(World world)
        {
            this._world = world;
            BodyDef bodyDef = new BodyDef();
            _body = world.CreateBody(bodyDef);
            _body.Owner = this;
            _body._userData = "ElementPiece";
            _body._type = BodyType.Static;
        }

        public override void Update(int delta)
        {
            if (IsGot)
            {
                base.Update(delta);
                if (player != null )
                {
                    if (player._playerState == PlayerState.Left)
                    {
                        Position = player.Position + new Vector2(-10, 0);
                    }
                    else if (player._playerState == PlayerState.Right)
                    {
                        Position = player.Position + new Vector2(10, 0);
                    }
                }
            }
        }

        public override void OnDraw(SpriteBatch spriteBatch, Matrix transform)
        {
           // if (!IsGot)
                base.OnDraw(spriteBatch, transform);
        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            //base.BeginContactAction(contact, contactBody, normal);
            if (contactBody.GetUserData() == "player" && !IsGot)
            {
                _filter.maskBits = 0x00;
                _fixture.SetFilterData(ref _filter);
                IsGot = true;
                _body._type = BodyType.Dynamic;
                player = (Player)contactBody.Owner;
                player.elementPieceList.Add(this);
            }
        }
    }
}
